<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Href\MessageHrefAction;
use App\Admin\Actions\ShopUser\ShopUserAdjustmentAction;
use App\Admin\Actions\ShopUser\ShopUserAgentAction;
use App\Admin\Actions\ShopUser\ShopUserPowerAction;
use App\Admin\Actions\ShopUser\ShopUserRefreshAction;
use App\Admin\Actions\ShopUser\ShopUserSystemAction;
use App\Admin\Grid\NGrid;
use App\Models\ShopUserModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid\Displayers\Actions;
use Encore\Admin\Grid\Filter;
use Encore\Admin\Show;

class ShopUserController extends AdminController
{
    protected $title = '用户管理';

    protected function grid(): NGrid
    {
        $grid = new NGrid(new ShopUserModel);
        $grid->model()->where('user_delete_flag', 2)->orderByDesc('user_id');
        $grid->filter(function (Filter $filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('user_id', 'ID')->integer();
                $filter->equal('user_mobile', __('l.user_mobile'));
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('user_type', __('l.type'))->select(__('l.userTypeIdArr'));
            });
        });
        $grid->column('user_id', 'ID')->sortable();
        $grid->column('user_avatar', __('shop_user.user_avatar'))->display(function ($column) {
            if (!is_null($column)) return view('image', ['id' => $this->user_id, 'image' => fileIdToUrl($column)]);
            else return null;
        });
        $grid->column('user_status', __('l.status'))->using(__('shop_user.statusArr'));
        $grid->column('user_type', __('l.type'))->using(__('l.userTypeIdArr'));
        $grid->column('user_sex', __('l.sex'))->using(__('l.sexArr'));
        $grid->column('user_main_id', __('l.user_main_id'));
        $grid->column('user_name', __('l.user_name'));
        $grid->column('user_mobile', __('l.user_mobile'));
        $grid->column('user_nickname', __('l.user_nickname'));
        $grid->column('user_peer_power', __('shop_user.power'))->using(__('shop_user.powerArr'));
        $grid->column('agent', __('shop_user.agent'))->using(__('l.whether'));
        $grid->column('is_system', __('shop_user.is_system'))->using(__('l.whether'));
        $grid->column('invite_code', __('l.invite_code'));
        $grid->column('user_delete_flag', __('shop_user.user_delete_flag'))->using(__('shop_user.deleteWhether'));
        $grid->column('created_at', __('l.createdAt'))->display(function ($column) {
            return now()->timestamp($column)->toDateTimeString();
        });
        $grid->disableActions(false);
        $grid->actions(function (Actions $action) {
            $action->disableDelete();
            $action->disableEdit();
            $action->disableView(false);
            if (config('app.superId') == Admin::user()->user_id && $action->row->user_delete_flag === 2) {
                $action->add(new ShopUserSystemAction($this->row->is_system));
                if ($action->row->user_type === 2) {
                    $action->add(new ShopUserAgentAction($this->row->agent));
                } else if ($action->row->user_type == 3) {
                    $action->add(new ShopUserPowerAction($this->row->user_peer_power));
                }
            }
            $action->add(new MessageHrefAction($action->row->user_id));
            $action->add(new ShopUserRefreshAction);
            if ($action->row->user_status === 1) $action->add(new ShopUserAdjustmentAction);
        });
        return $grid;
    }

    /**
     * detail
     * @param int $id
     * @return Show
     */
    protected function detail(int $id): Show
    {
        $show = new Show(ShopUserModel::findOrFail($id));
        $show->field('user_id', 'ID');
        $show->field('user_avatar', __('shop_user.user_avatar'))->unescape()->as(function ($column) {
            if (!is_null($column)) return view('image', ['image' => fileIdToUrl($column)]);
            else return null;
        });
        $show->field('user_status', __('l.status'))->using(__('shop_user.statusArr'));
        $show->field('user_type', __('l.type'))->using(__('l.userTypeIdArr'));
        $show->field('user_sex', __('l.sex'))->using(__('l.sexArr'));
        $show->field('user_main_id', __('l.user_main_id'));
        $show->field('user_name', __('l.user_name'));
        $show->field('user_mobile', __('l.user_mobile'));
        $show->field('user_nickname', __('l.user_nickname'));
        $show->field('user_shop_name', __('shop_user.user_shop_name'));
        $show->field('user_shop_status', __('shop_user.user_shop_status'))->using(__('shop_user.userShopStatusArr'));
        $show->field('user_shop_time', __('shop_user.user_shop_time'))->unescape()->as(function ($column) {
            if ($column === 0) return null;
            else return now()->timestamp($column)->toDateTimeString();
        });
        $show->field('user_delete_flag', __('shop_user.user_delete_flag'))->using(__('shop_user.deleteWhether'));
        $show->field('user_peer_power', __('shop_user.power'));
        $show->field('agent', __('shop_user.agent'))->using(__('l.whether'));
        $show->field('is_system', __('shop_user.is_system'))->using(__('l.whether'));
        $show->field('invite_code', __('l.invite_code'));
        $show->field('created_at', __('l.createdAt'))->unescape()->as(function ($column) {
            return now()->timestamp($column)->toDateTimeString();
        });
        return $show;
    }
}
